
package com.shop.cloud.mall.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.mall.api.service.SeckillHallInfoService;
import com.shop.cloud.mall.api.service.SeckillHallService;
import com.shop.cloud.mall.api.service.SeckillInfoService;
import com.shop.cloud.mall.common.constant.MallReturnCode;
import com.shop.cloud.mall.common.entity.SeckillHall;
import com.shop.cloud.mall.common.entity.SeckillHallInfo;
import com.shop.cloud.mall.common.entity.SeckillInfo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 秒杀商品
 *
 * @author
 * @date 2020-08-12 16:16:45
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/seckillinfo")
@Tag(description = "seckillinfo", name = "秒杀商品API")
public class SeckillInfoController {

    private final SeckillInfoService seckillInfoService;

	private final SeckillHallInfoService seckillHallInfoService;

	private final SeckillHallService seckillHallService;

	/**
	 * 分页列表
	 * @param page 分页对象
	 * @param seckillInfo
	 * @return
	 */
	@Operation(summary = "分页列表")
	@GetMapping("/page")
	public R getPage(Page page, SeckillInfo seckillInfo, SeckillHallInfo seckillHallInfo) {
		seckillInfo.setEnable(CommonConstants.YES);
		return R.ok(seckillInfoService.page2(page, seckillInfo, seckillHallInfo));
	}

	/**
	 * 秒杀商品查询
	 * @param seckillHallInfoId
	 * @return R
	 */
	@Operation(summary = "秒杀商品查询")
	@GetMapping("/{seckillHallInfoId}")
	public R getById(@PathVariable("seckillHallInfoId") String seckillHallInfoId) {
		SeckillHallInfo seckillHallInfo = seckillHallInfoService.getById(seckillHallInfoId);
		if(seckillHallInfo == null){
			return R.failed(MallReturnCode.ERR_80046.getCode(), MallReturnCode.ERR_80046.getMsg());
		}
		//查询秒杀商品
		SeckillInfo seckillInfo = seckillInfoService.getById2(seckillHallInfo.getSeckillInfoId());
		//查询会场信息
		SeckillHall seckillHall  = seckillHallService.getById(seckillHallInfo.getSeckillHallId());
		seckillInfo.setSeckillHall(seckillHall);
		return R.ok(seckillInfo);
	}
}
